# -*- coding: utf-8 -*-

from pydantic import BaseModel, EmailStr

from fastapi_mail import FastMail, ConnectionConfig, MessageSchema, MessageType

from config.env import EmailConfig


emial_connection_config = ConnectionConfig(
    MAIL_USERNAME=EmailConfig.email_username,
    MAIL_PASSWORD=EmailConfig.email_password,
    MAIL_FROM=EmailConfig.email_from,
    MAIL_PORT=EmailConfig.email_port,
    MAIL_SERVER=EmailConfig.email_server,
    MAIL_SSL_TLS=EmailConfig.email_ssl_tls,
    MAIL_STARTTLS =EmailConfig.email_starttls,  # 是否使用SSL: 587
    USE_CREDENTIALS=EmailConfig.email_use_credential,  # 是否使用认证
    VALIDATE_CERTS=EmailConfig.email_validate_cert,  # 是否验证证书
)

class VerifyEmailCode(BaseModel):
    email: EmailStr
    code: str

class Email(BaseModel):
    addresses: list[EmailStr]
    subject: str
    content: str


async def send_message(email: Email):
    
    body = f"""
    <h1>{email.subject}</h1>
    <p>{email.content}</p>
    """

    message = MessageSchema(
        subject=email.subject,
        recipients=email.addresses,
        body=body,
        subtype=MessageType.html
    )

    fm = FastMail(emial_connection_config)

    await fm.send_message(message) 
